<?php

class Libs_Paging {

    private $_records;
    private $_max_pp;
    private $_numb_of_pages;
    private $_numb_of_records;
    private $_current;
    private $_offset = 0;
    private static $_key = 'page';
    public $_url;
    public $_qm;

    public function __construct($url, $rows, $max = 10) {
        $this->_records = $rows;
        $this->_numb_of_records = count($this->_records);
        $this->_max_pp = $max;
        $this->_url = preg_replace('/[?&]page=.*/', '', $url);
        if (strpos($this->_url, "?")) {
            $this->_qm = "&";
        } else {
            $this->_qm = "?";
        }
        $current = $_GET[self::$_key];
        $this->_current = !empty($current) ? $current : 1;
        $this->numberOfPages();
        $this->getOffset();
    }

    private function numberOfPages() {
        $this->_numb_of_pages = ceil($this->_numb_of_records / $this->_max_pp);
    }

    private function getOffset() {
        $this->_offset = ($this->_current - 1) * $this->_max_pp;
    }

    public function getRecords() {
        $out = array();
        if ($this->_numb_of_pages > 1) {
            $last = ($this->_offset + $this->_max_pp);

            for ($i = $this->_offset; $i < $last; $i++) {
                if ($i < $this->_numb_of_records) {
                    $out[] = $this->_records[$i];
                }
            }
        } else {
            $out = $this->_records;
        }
        return $out;
    }

    private function getLinks() {
        if ($this->_numb_of_pages > 1) {
            $out = array();

            //first link
            if ($this->_current > 1) {
                $out[] = "<a href=\"" . $this->_url . "\">Đầu</a>";
            } else {
                $out[] = "<span class='disabled'>Đầu</span>";
            }

            //previour link
            if ($this->_current > 1) {

                //previous page number
                $id = ($this->_current - 1);

                $url = $id > 1 ?
                        $this->_url . $this->_qm . self::$_key . "=" . $id :
                        $this->_url;
                $out[] = "<a href=\"{$url}\">Trước</a>";
            } else {
                $out[] = "<span class='disabled'>Trước</span>";
            }

            if (!empty($this->_numb_of_pages)) {
                $spilit = 2;
                $begin = $this->_current - $spilit;
                $end = $this->_current + $spilit;
                if ($begin < 1) {
                    $begin = 1;
                    $end = $spilit * 2 + 1;
                }
                if ($end > $this->_numb_of_pages) {
                    $end = $this->_numb_of_pages;
                    $begin = $end - ($spilit * 2);
                    if ($begin < 1) {
                        $begin = 1;
                    }
                }
                for ($begin; $begin <= $end; $begin++) {
                    if ($this->_current == $begin) {
                        $out[] = "<span class='selected'>$begin</span>";
                    } else {
                        $out[] = "<a href='$this->_url$this->_qm" . self::$_key . "=$begin'>$begin</a>";
                    }
                }
            }

            //next link
            if ($this->_current != $this->_numb_of_pages) {
                //next page number
                $id = ($this->_current + 1);

                $url = $this->_url . $this->_qm . self::$_key . "=" . $id;
                $out[] = "<a href=\"{$url}\">Tiếp</a>";
            } else {
                $out[] = "<span class='disabled'>Tiếp</span>";
            }

            //last link
            if ($this->_current != $this->_numb_of_pages) {
                $url = $this->_url . "?" . self::$_key . "=" . $this->_numb_of_pages;
                $out[] = "<a href=\"{$url}\">Cuối</a>";
            } else {
                $out[] = "<span class='disabled'>Cuối</span>";
            }
            return "<span>" . implode("</span><span>", $out) . "</span>";
        }
    }

    public function getPaging() {
        $link = $this->getLinks();
        if (!empty($link)) {
            $out = "<div class=\"pagination\">";
            $out .= $link;
            $out .= "</div>";
            return $out;
        }
    }

}

?>
